layui.use(['jquery', 'form', 'common', 'layer'], function(){
	var $ = layui.jquery;
	var form = layui.form;
	var common = layui.common;
	var layer = layui.layer;

	var refreshCaptcha = function() {
		var time = new Date().getTime();
		$('#LAY-user-get-vercode').prop('src', common.ctxPath + '/captcha?time=' + time);
	};
	$('#LAY-user-get-vercode').on('click', function(data) {
		refreshCaptcha();
	});
	form.on('submit', function(data) {
		$.post(common.ctxPath + '/doLogin', data.field, function(data) {
			if (data.state == 'ok') {
				// 成功，跳转页面
				layer.msg(data.msg, {
					icon: 6,
					time: 2000
				}, function() {
					common.toUrl(data.url);
				});
			} else {
				// 失败，弹出提示
				layer.msg(data.msg, {
					icon: 5,
					anim: 6
				});
			}
			if (data.requestCaptcha) {
				// 显示验证码
				$('#captchaContainer').removeClass('layui-hide');
			}
			if (data.refreshCaptcha) {
				// 刷新验证码
				refreshCaptcha();
			}
		}, 'json');
		return false;
	});
	// 进入页面提示
	var msg = $('#msg').val();
	if (msg) {
		layer.ready(function() {
			layer.msg(msg, {
				icon: 0
			});
		});
	}
});
